home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2321 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  140 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: Problem with program 
  5. X-Nntp-Posting-Host: golden.ripco.com
  6. Message-ID: <DLHGws.3q9@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Sat, 20 Jan 1996 14:14:51 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. elvemo@sn.no (Rune Elvemo)
  13. in <4dkr5u$ro7@hasle.sn.no> asks:
  14.  
  15. >Anyone who can figure out what is wrong with this program?
  16.  
  17. Logically, there are lots of problems.  The obvious syntax errors are fixed
  18. below.
  19.  
  20. >I tried to compile it, but my compiler wouldn't.....
  21.  
  22. Of course not.
  23.  
  24. >Ex: It said that the prototype needed a semicolon, but it
  25. >don't.........
  26.  
  27. Well, you have to know where to look.  In this case, the declaration of the
  28. struct Text needs a semicolon.  My changes to your code are marked with
  29. "/* mha ... */" comments.
  30.  
  31.  
  32. /* Loadfile.c - loads a txt file, and shows it */
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>             /* mha - added */
  36.  
  37. struct Text {
  38.     char *str;
  39.     struct Text *next;
  40. };                              /* mha - added ';' */
  41.  
  42. typedef enum {
  43.     TRUE, FALSE
  44. }   BOOL;                       /* mha - was "enum BOOL {TRUE,FALSE};" */
  45.  
  46.  /* prototype */
  47. void PText(struct Text *);
  48.  
  49. int /* mha - added return type */ main(int argc, char *argv[])
  50. {
  51.     BOOL done = FALSE;
  52.     FILE *txtfile;
  53.     struct Text *first, *tst;
  54.  
  55.     if (argc > 1) {
  56.         if ((txtfile = fopen(argv[1], "r"))) {  /* mha - added () */
  57.             if ((first = malloc(sizeof(struct Text)))) {
  58.                 /* mha - added (). Replaced "calloc(1, sizeof(struct
  59.                  * Text))".  Usually calloc is wasted motion. */
  60.                 tst = first;
  61.                 if (fgetc(txtfile) != EOF) {
  62.                     /* mha - This did have a test with -1, a very bad
  63.                      * practice.  Notice that this code loses the
  64.                      * character obtained.  Is this what you want? */
  65.                     while (!done) {
  66.                         if ((tst->str = malloc(256))) {
  67.                             /* mha - added (). Replaced "calloc(256,
  68.                              * sizeof(char))".  Not only is calloc
  69.                              * wasted motion, but also sizeof(char) is
  70.                              * just typing practice. The hardcoded 256
  71.                              * is still a problem */
  72.                             do {
  73.                                 *tst->str = fgetc(txtfile);
  74.                                 tst->str++;
  75.                                 *tst->str = 0;
  76.                                 /* check if the last char was LineFeed
  77.                                  * or EOF  */
  78.  
  79.                             } while ((*(tst->str - 1) != EOF)   /* mha - was
  80.                                          non-standard -1 */ &&
  81.                                      (*(tst->str) != '\n')  /* mha - was
  82.                                          non-standard 10 */ );
  83.  
  84.                             if (*tst->str == EOF)   /* mha - was
  85.                                                      * non-standard -1 */
  86.                                 done = TRUE;
  87.                             else {
  88.                                 if ((tst->next = malloc(sizeof(struct Text)))) {
  89.                                     /* mha - added () - was "calloc(1,
  90.                                      * sizeof(struct Text))" */
  91.                                     tst = tst->next;
  92.  
  93.                                 } else {
  94.                                     done = TRUE;
  95.                                     printf("\n**!!Not enough memory!!**\n");
  96.                                     /* mha - stderr exists for a
  97.                                      * reason.  Try using it */
  98.                                 }
  99.                             }
  100.                         }
  101.                     }
  102.                     tst->next = malloc(sizeof(struct Text));
  103.                     /* mha - was "calloc(1, sizeof(struct Text))".
  104.                      * calloc() is the wrong way to make sure that
  105.                      * pointers in the allocated struct are set to
  106.                      * NULL.  Do it yourself.  No extra points for
  107.                      * being lazy */
  108.                     PText(first);
  109.                 }
  110.             } else
  111.                 printf("\n**!!Not enough memory!!**\n");
  112.         }
  113.     }
  114.     return 0;                   /* mha - added explicit return */
  115. }
  116.  
  117. /* Print the text that we have got */
  118. void PText(struct Text * txt)
  119. {
  120.     BOOL done = FALSE;
  121.     struct Text *jump;
  122.     jump = txt;
  123.     while (!done) {
  124.         printf("%s", jump->str);
  125.         while (txt->next != NULL)
  126.              /* mha - was "while (*txt->next != 0)" */ {
  127.             jump = jump->next;
  128.             printf("%s\n", jump->str);
  129.             /* mha - was 'printf("%s\n", jump);'.  No specification for
  130.              * printf ever promised that "%s" was appropriate for a
  131.              * "struct Text *" */
  132.             }
  133.     }
  134. }
  135.  
  136.                                                                                                             
  137. --
  138. * Martin Ambuhl       net: mambuhl@ripco.com
  139. * Chicago, IL (USA)    
  140.